<?php
class User_IndexController extends Zend_Controller_Action{

	public function init(){
		parent::init();

	}

	public function indexAction(){
		//		$layoutPath = APPLICATION_PATH  . '/templates/admin/default';
		//		$option = array ('layout' => 'index',
		//                   'layoutPath' => $layoutPath );
		//		Zend_Layout::startMvc ( $option );
		// Current user homepage is order page.
		$this->_forward('order');
	}


	public function scheduleAction() {
		$sid = $this->getRequest()->getParam('schedule');
		$oSchedule = new Admin_Model_ScheduleMapper();
		$scheduleForm = new User_Form_ScheduleSelect();
		$scheduleForm->setMethod('get');
		$arrSchedule = $oSchedule->fetchAll();
		$scheduleForm->bindData($arrSchedule);
		if (isset($sid)) {

		} else {
			if(isset($arrSchedule)) {
				$defaultSchedule = $arrSchedule[0];
				$sid = $defaultSchedule->_sid;
			}
		}
		if ($sid) {
			$scheduleInfo = new Admin_Model_Schedule();
			$this->view->scheduleForm = $scheduleForm;
			$has = $oSchedule->find($sid, $scheduleInfo);
			if (isset($has)) {
				$scheduleForm->populateData($scheduleInfo);
				$menuSchedule = $oSchedule->findmenu($sid);
				$this->view->menuSchedule = $menuSchedule;
			}
		}


	}

	protected function buildOrders($uid) {
		$orderMapper = new User_Model_OrderMapper();
		$orders = $orderMapper->getOrdersNotDone($uid);
		$userMapper = new Front_Model_UserMapper();
		$userInfo  = new Front_Model_User();
		$hasUser = $userMapper->find($uid, $userInfo);
		if (isset($hasUser)) {
			$this->view->remain = $userInfo->_remain;
			$today = new Zend_Date();
			$this->view->today = $today->toString();
		}
		if (isset($orders)) {
			$rowdata = array();
			$oids = array();
			foreach ($orders as $order) {
				if (Zend_Date::isDate($order->_date, DATE_FORMAT)) {
					$date = new Zend_Date();
					$date->setDate($order->_date, DATE_FORMAT);
					$now = new Zend_Date();
					if ($date->getDate()->isLater($now->getDate()) || $date->getDate()->isToday()) {
						$row = array();
						$oids[] = $order->_oid;
						$menu = $orderMapper->getMenu($order->_oid);
						$row['date'] = $order->_date;

						$row['menuname'] = $menu->_menuname;
						$row['menuimage'] = $menu->_menuimage;

						$typeInfo = $orderMapper->getType($order->_oid);
						$type = '';
						if (isset($typeInfo)) {
							$type = $typeInfo->_typename;
						}

						$row['type'] = $type;
						$row['quantity'] = $order->_quantity;
						$checked = 'false';
						if ($order->_status == 0) {
							$checked = 'checked';
						} else if ($order->_status == 1) {
							$checked = '';
						}
						$status = "<input type='checkbox' ".$checked."
							name='".$order->_oid."'/>";
						$row['status'] = $status;
						$rowdata[] = $row;
					}
				}
			}
			$this->view->oids =  implode(',',$oids);
			$this->view->rowdata  = $rowdata;
		}
	}

	protected function checkValidTime() {
		$now = new Zend_Date();
		$date = new Zend_Date('1990-01-07 10:00:00', 'yyyy-MM-dd HH:mm:ss');
		if ($now->getTime()->isLater($date->getTime())) {
			return false;
		}
		return true;
	}

	public function orderAction() {
		if ($this->checkValidTime()) {
			$auth = Zend_Auth::getInstance();
			$indentify = $auth->getIdentity();
			if (isset($indentify)) {
				$uid = $indentify->uid;
				if (isset($uid)) {
					if ($this->getRequest()->isPost()) {
						$oids = $this->getRequest()->getParam('oids');
						$oids = explode(',', $oids);
						if (is_array($oids)) {
							$orderMapper = new User_Model_OrderMapper();
							$userMapper = new Front_Model_UserMapper();
							foreach ($oids as $oid) {
								$order = true;
								if (!isset($_POST[$oid])) {
									$order = false;
								}
								$orderInfo = new User_Model_Order();
								if ($orderMapper->find($oid, $orderInfo)) {
									$userinfo = new Front_Model_User();
									$hasUser = $userMapper->find($orderInfo->_uid, $userinfo);
									$status = 0;
									$newRemain = $userinfo->_remain;
									if ($order) {
										$status = 0;
										$newRemain = $newRemain  - $orderInfo->_quantity;
									} else {
										$status = 1;
										$newRemain = $newRemain  + $orderInfo->_quantity;
									}

									if ($status != $orderInfo->_status) {
										// update remain
										if (isset($hasUser)) {
											$userinfo->_remain = $newRemain;
											$userMapper->save($userinfo);
										}
									}
									$this->updateStatus($oid, $status, $orderMapper);
								}
							}
						}
					}
					$this->buildOrders($uid);
				}
			}
		}
		else {
			echo "You can't order at now";
		}

	}

	protected function updateStatus($oid, $status, User_Model_OrderMapper $orderMapper) {
		$orderInfo = new User_Model_Order();
		if ($orderMapper->find($oid, $orderInfo) ) {
			$orderInfo->_status = $status;
			$orderMapper->save($orderInfo);
		}
	}

}